<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/29
 * Time: 10:19
 */
namespace app\province\controller;
use think\Db;
class User  extends Base
{
    /*
     * 用户列表
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $where['f_id'] =session('p_uid');
            $factory_id = Db::table('up_factory')->where($where)->column('factory_id');
            $map = [];

            if(isset($key)&&$key!="")
            {
                $map['uf.factory_name'] = ['like',"%" . $key . "%"];
            }
            //厂家名称
            if(isset($fid)&&$fid!="")
            {
                $map['uf.factory_id'] = $fid;
            }
            //时间筛选
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.m_create_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.m_create_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.m_create_time'] = ['between time',[$start,$end]];
            }
            $map['a.m_factory_id'] = array('IN',$factory_id);
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_factory_member')->alias('a')
                ->join('up_factory uf','uf.factory_id = a.m_factory_id','left')
                ->join('up_province up','up.p_id = uf.f_id','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_factory_member')
                ->alias ('a')
                ->join('up_factory uf','uf.factory_id = a.m_factory_id','left')
                ->join('up_province up','up.p_id = uf.f_id','left')
                ->page($page, $limit)
                ->where($map)
                ->field('a.*,up.p_username,uf.factory_name')
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $factory_info = Db::table('up_factory')->field('factory_id,factory_name')->select();
        $this->assign('factory_info',$factory_info);
        return $this->fetch();
    }

    /*
     * 注销账号
     */
//    public function userOut(){
//        Db::startTrans ();
//        try{
//            Db::name('process')->where('p_uid',input('id'))->delete();
//            Db::commit ();
//            return json(['code'=>200,'msg'=>'注销成功']);
//        }catch (\Exception $e){
//            Db::rollback ();
//            return json(['code'=>100,'msg'=>'注销失败']);
//        }
//    }
    /****
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $res = Db::table('up_factory_member')->where('m_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    public function add(){
        if(request()->isPost()){
            $len = mb_strlen(input('param.m_name'),'utf8');
            $m_serial = mb_strlen(input('param.m_serial'),'utf8');
            if($len>5){
                return json(['code' => 100, 'data' => '', 'msg' => '不能超过5个字符']);
            }
            if($m_serial>30){
                return json(['code' => 100, 'data' => '', 'msg' => '用户编号超过5个字符']);
            }
            $m_id = Db::table('up_factory_member')->where('m_serial',input('param.m_serial'))->value('m_id');
            if($m_id){
                return json(['code' => 100, 'data' => '', 'msg' => '不能添加相同的用户编号']);
            }
            $data['m_serial'] = input('param.m_serial');
            $data['m_name'] = input('param.m_name');
            $data['m_factory_id'] = input('param.m_factory_id');
            $data['m_create_time'] = date('Y-m-d H:i:s',time());
            $res =  Db::table('up_factory_member')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $where['f_id'] =session('p_uid');
        $data1 = Db::table('up_factory')->where($where)->field('factory_id,factory_name')->select();
        $this->assign('data1',$data1);
        return $this->fetch();
    }

    /**
     * editPwd 修改管理员密码
     * @return \think\response\Json
     */
    public function editPwd(){
        extract(input());
        if(isset($type) && $type=='checkPassword'){
            $old_pwd = md5($old_pwd);
            $password =  Db::name('province')->where("p_id",session('f_uid'))->value("p_password");
            if($password === $old_pwd){
                return json(['code' => 200, 'data' => '', 'msg' =>'true']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' =>'false']);
            }
        }else{
            $new_pwd = md5($new_pwd);
            $res =  Db::name('province')->where("p_id",session('f_uid'))->setField ('p_password',$new_pwd);
            if($res){
                return json(['code'=>200,'msg'=>'密码修改成功，请重新登录！']);
            }else{
                return json(['code'=>100,'msg'=>'密码修改失败']);
            }
        }
    }
}